<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/node_modules/mditor/dist/css/mditor.min.css" />
    <script src="/node_modules/jquery/dist/jquery.min.js"></script>
    <script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="/node_modules/mditor/dist/js/mditor.min.js"></script>

</head>

<body>
    <%- include('../layout/header.ejs')%>
    <div class="container-fluid">
        <h1>发表文章</h1>
        <hr>

        <form id="form">
            <!-- <input type="hidden" name="authorId" id="authorId" value="<%= user.id %>"> -->
            <div class="form-group">
                <label for="">文章标题：</label>
                <input type="text" name="title" id="title" class="form-control" required maxlength="30">
            </div>

            <div class="form-group">
                <label for="">文章内容：</label>
                <!-- 添加 textarea 元素 -->
                <textarea name="content" id="editor" class="form-control"></textarea>
            </div>

            <div class="form-group clearfix">
                <input type="submit" value="发表文章" class="btn btn-primary pull-right">
            </div>
        </form>
    </div>
    <%- include('../layout/footer.ejs')%>
</body>
<script>
    $(function () {

        // 创建Mditor实例   readme中可以进行各种配置
        let mditor = Mditor.fromTextarea(document.getElementById('editor'))

        $('#form').on('submit', function (e) {
            e.preventDefault()
            $.ajax({
                type: "post",
                url: "/article/add",
                data: {
                    title: $('#title').val(),
                    content: mditor.value,
                    authorId: '<%= user.id %>'
                },
                dataType: "json",
                success: function (result) {
                    console.log(result);
                    if (result.status === 1) {
                        location.href = '/article/info?id=' + result.id
                    } else {
                        alert(result.msg)
                    }
                }
            });
        })
    })
</script>

</html>